package com.novoda.runbuddy.view;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;

import com.novoda.runbuddy.R;
import com.novoda.runbuddy.controller.IStopClockController;
import com.novoda.runbuddy.provider.ActivityID;

public class StopwatchView implements ViewFactory {

	private Context					context;
	private TextSwitcher			stopClock;
	private Button					startStopButton;
	private IStopClockController	stopwatchController;

	public StopwatchView(Context context, IStopClockController controller, TextSwitcher stopClock, Button startStopButton) {
		this.context = context;
		this.stopClock = stopClock;
		this.startStopButton = startStopButton;
		this.stopwatchController = controller;

		stopClock.setFactory(this);

		this.startStopButton.setOnClickListener(executeAStart);

	}

	public TextSwitcher getStopWatch() {
		return this.stopClock;
	}

	public void resetStopClock() {
		this.stopClock.setText(ActivityID.START_TIME);
	}

	public View makeView() {
		TextView textView = new TextView(context);
		textView.setGravity(Gravity.CENTER);
		textView.setTextSize(50);
		return textView;
	}

	public void setStopClock(String text) {
		this.stopClock.setText(text);
	}

	public void switchStopwatchButton(int stopClockState) {

		if (stopClockState == ActivityID.STOPWATCH_RUNNING) {
			startStopButton.setText(R.string.button_stop);
			setStartStopButtonClickListener(executeAStop);
		} else {
			startStopButton.setText(R.string.button_start);
			setStartStopButtonClickListener(executeAStart);
		}
	}

	private OnClickListener	executeAStart	= new OnClickListener()
											{
												public void onClick(View v) {
													stopwatchController.start();
												}
											};

	private OnClickListener	executeAStop	= new OnClickListener()
											{
												public void onClick(View v) {
													stopwatchController.stop();
												}
											};

	public void setStartStopButtonClickListener(OnClickListener onClickListener) {
		this.startStopButton.setOnClickListener(onClickListener);
	}

}
